'''
Created on Sep 2, 2012

@author: kingsfield
'''
import csv
from util import SQL_CONF
from collections import defaultdict
import MySQLdb as mdb

class check(object):
    '''
    classdocs
    '''


    def __init__(self, params):
        '''
        Constructor
        '''
        
wd = "/home/kingsfield/Desktop/kaggle/"        
        
if __name__ == '__main__':
    con = None
    con = mdb.connect(SQL_CONF.LOCATION, SQL_CONF.USER, SQL_CONF.PASSWD, SQL_CONF.DATABASE)
    cur = con.cursor()
    cur.execute("SELECT VERSION()")
    data = cur.fetchone()
    print "Init database done, version : %s " % data
    
    hot_sql = 'select * from (select count(1) as c,max(cat) as cat, sku from train group by sku) t0 order by t0.c DESC limit 50'
    print hot_sql
    cur.execute(hot_sql)
    hot = cur.fetchall()
    for line in hot:
        count, cat, sku = line
        query_sql = 'select * from (select count(1) as c, query from train where sku=%s group by query) t0 order by t0.c DESC limit 10' % sku
        cur.execute(query_sql)
        query = cur.fetchall()
        print '********************* %s=%d %s ********************' % (sku, count, cat)
        for q in query:
            q_count, q_query = q
            print '     %d,  %s' % (q_count, q_query)
    
    print 'done'        
